import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

import ru.ksu.ktk.diploma.sdi.analyzer.contingency.NominalData;
import ru.ksu.ktk.diploma.sdi.mapping.loaders.ThesauriLoader;
import ru.ksu.ktk.diploma.sdi.util.OntClassUtil;

import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;

@ContextConfiguration(locations = {"classpath:META-INF/spring/mapping-beans.xml", "classpath:test-dao-beans.xml",
    "classpath:test-properties-beans.xml", "classpath:META-INF/spring/analyzer-beans.xml"})
public class FacetUpcastingTestCase extends AbstractTestNGSpringContextTests {
    private static final String FOLDER_NAME = "C://temp//emku//desc";

    private static final String[] SUPER_FACET_NAMES =
        {"300185711", "300241489", "300241490", "300264550", "300264551", "300264552"};

    private static final String FIRST_SUPER_FACETS_VECTOR_PATH =
        String.format("%s//%s", FOLDER_NAME, "emku-name-tn-superfacets.txt");

    private static final String SECOND_SUPER_FACETS_VECTOR_PATH =
        String.format("%s//%s", FOLDER_NAME, "emku-technique-nm-filtered.txt");

    private static final String NOMINAL_DATA_PATH =
        String.format("%s//%s", FOLDER_NAME, "emku-desc-name-technique.txt");

    private static final String OUTPUT_FILE_PATH =
        String.format("%s//%s", FOLDER_NAME, "emku-desc-name-technique-upcasted.txt");

    @Autowired
    ThesauriLoader thesauriLoader;

    OntModel thesauri;

    @Test(groups = "facetVector")
    public void testFacetUpcasting() throws FileNotFoundException, IOException {
        thesauri = thesauriLoader.load();

        String vectorFilePath = String.format("%s//%s", FOLDER_NAME, "emku-name-tn-filtered.txt");
        List<String> descList = FileUtil.readValues(vectorFilePath);
        List<String> superFacets = new ArrayList<String>();
        for (String desc : descList){
            String superFacetUri = getSuperFacetUri(desc, Arrays.asList(SUPER_FACET_NAMES));
            if (superFacetUri != null && !superFacets.contains(superFacetUri)){
                superFacets.add(superFacetUri);
            }
        }
        String outputFilePath = String.format("%s//%s", FOLDER_NAME, "emku-name-tn-superfacets.txt");
        FileUtil.write(superFacets, outputFilePath);
    }

    @Test(groups = "facetPairVector")
    public void testFacetPairUpcasting() throws FileNotFoundException, IOException {
        thesauri = thesauriLoader.load();
        List<String> firstFieldSuperFacets = FileUtil.readValues(FIRST_SUPER_FACETS_VECTOR_PATH);

        List<String> secondFieldSuperFacets = FileUtil.readValues(SECOND_SUPER_FACETS_VECTOR_PATH);

        List<NominalData> nominalDataList = FileUtil.readNominalData(NOMINAL_DATA_PATH);

        List<NominalData> newNominalDataList = new ArrayList<NominalData>();

        for (NominalData nominal : nominalDataList){
            String firstDesc = nominal.getFirstNominalValue();
            String firstSuperFacetUri = getSuperFacetUri(firstDesc, firstFieldSuperFacets);
            String secondDesc = nominal.getSecondNominalValue();
            String secondSuperFacetUri = getSuperFacetUri(secondDesc, secondFieldSuperFacets);
            if (firstSuperFacetUri != null && secondSuperFacetUri != null){
                NominalData newNominal = new NominalData(firstSuperFacetUri, secondSuperFacetUri, nominal.getCount());
                int nominalIndex = newNominalDataList.indexOf(newNominal);
                if (nominalIndex != -1){
                    NominalData foundNominalData = newNominalDataList.get(nominalIndex);
                    foundNominalData.increaseCount(newNominal.getCount());
                }
                else{
                    newNominalDataList.add(newNominal);
                }

            }

        }
        FileUtil.writeNominalData(newNominalDataList, OUTPUT_FILE_PATH);
    }

    private String getSuperFacetUri(String descBlockUri, List<String> superFacetNameList) {
        OntClass descBlock = thesauri.getOntClass(descBlockUri);
        List<OntClass> superClasses = OntClassUtil.getAllSuperClasses(descBlock);
        List<OntClass> classList = new ArrayList<OntClass>();
        classList.add(descBlock);
        classList.addAll(superClasses);
        for (OntClass ontClass : classList){
            String ontClassUri = ontClass.getURI();
            for (String str : superFacetNameList){
                if (ontClassUri.contains(str)) return ontClassUri;
            }
        }
        return null;
    }
}
